%
%  Raises x to the power of 2. x must be in two column interval format, and thus convex.
%  This is distinct from a fuzzy multiplication of x times x in that it guarantees that
%  the result is entirely positive, i.e. the span{y} \in [0,inf).  
%  This is agnostic to the implicit alpha values of the intervals.
%  It is noteworthy that the assumption of the positiveness of the result improves performance
%  by significantly reducing the number of cases needed to check when doing interval multiplication.
%
function y = fuzzy_square(x)

l = x(:,1);
r = x(:,2);

temp = [l .* l, l .* r, r .*r];

ln = zeros([length(l),1]);
rn = zeros([length(r),1]);

indices = intersect(find(l >= 0), find(r >= 0)); 
ln(indices) = l(indices) .* l(indices);
rn(indices) = r(indices) .* r(indices);

indices = intersect(find(l < 0), find(r >= 0)); 
rn(indices) = max(abs(temp(indices,:).')).';

indices = intersect(find(l < 0), find(r < 0)); 
ln(indices) = r(indices) .* r(indices);
rn(indices) = l(indices) .* l(indices);

y = [ln, rn];
